package com.xd.server.student.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xd.common.page.Pagination;
import com.xd.common.utils.OrikaUtils;
import com.xd.server.student.dto.StudentDTO;
import com.xd.server.student.mapper.IStudentMapper;
import com.xd.server.student.po.StudentPO;
import com.xd.server.student.service.IStudentService;
import com.xd.server.student.vo.StudentVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * (Student)表服务实现类
 *
 * @author XD
 * @since 2023-10-10 09:04:05
 */
@Service("studentService")
@Slf4j
public class StudentServiceImpl extends ServiceImpl<IStudentMapper, StudentPO> implements IStudentService {
    @Resource
    private IStudentMapper studentMapper;

    /**
     * 增加
     *
     * @param studentDTO DTO传输类
     */
    @Override
    public int saveOrUpdateStudent(StudentDTO studentDTO) {
        // 根据类型特殊处理
        String id = studentDTO.getId();
        final StudentPO studentPO = OrikaUtils.map(studentDTO, StudentPO.class);
        if (Objects.isNull(id)) {
            // 新增
            return studentMapper.insert(studentPO);
        } else {
            // 修改
            return studentMapper.updateById(studentPO);
        }


    }

    /**
     * 删除
     *
     * @param id 主键
     */
    @Override
    public int deleteStudent(String id) {
        return studentMapper.deleteById(id);

    }


    /**
     * 查询一个
     *
     * @param id 主键
     * @return com.xd.vo.StudentVO
     */
    @Override
    public StudentVO findStudent(String id) {
        final StudentPO studentPO = studentMapper.selectById(id);
        return OrikaUtils.map(studentPO, StudentVO.class);
    }

    /**
     * 查询所有
     *
     * @return java.util.List
     */
    @Override
    public List<StudentVO> findAllStudent() {
        final List<StudentPO> studentPOS = studentMapper.selectList(null);
        return OrikaUtils.mapAsList(studentPOS, StudentVO.class);
    }

    /**
     * 查询所有-分页查询
     *
     * @return java.util.List
     */
    @Override
    public Page<StudentVO> findPageStudent(StudentDTO studentDTO) {
        Page<StudentVO> page = Pagination.page(studentDTO);
        List<StudentVO> StudentVOS = studentMapper.findAll(page, studentDTO);
        return page.setRecords(StudentVOS);
    }

}
